<script setup>
import { onMounted, reactive, ref } from "vue";
import addForm from "./addForm.vue";
import Table from "../../../components/Table/table";
import Pagination from "@/components/Pagination/Pagination";
import { useCommunityNameList, useRepairStatus } from "@/hooks/index";
import { repairTable } from "@/hooks/repair";
import ImagePreview from "@/components/ImagePreview";
import { repairListApi } from "../../../api/repair";
import { delRepairInfoApi } from "../../../api/repair";
import { ElMessage } from "element-plus";
import { formatTimes } from "../../../utils/Fw-data";
// ===================================================================

const getCommunityNameList = useCommunityNameList();
const getRepairStatus = useRepairStatus();
const addRef = ref();
const list = ref([]);
const total = ref(0);

const queryData = reactive({
  current: 1,
  pageSize: 10,
  create_time: "",
  community_code: "",
  status: "",
});

onMounted(() => {
  initData();
});

// 表格功能
const initData = async () => {
  let {
    data: { count, rows },
  } = await repairListApi(queryData);
  list.value = rows.map(({ qu_photo, ...item }) => ({
    qu_photo:
      import.meta.env.VITE_APP_BASE_IMG +
      (qu_photo
        ? qu_photo.split(",")[0]
        : "/public/dist/static/empty.1e2fbc24.png"),
    ...item,
  }));
  // list.value = rows;
  total.value = count;
};

// 搜索
const onSearch = () => {
  queryData.current = 1;
  initData();
};
// 搜索取消
const onReset = () => {
  Object.assign(queryData, {
    current: 1,
    pageSize: 10,
    create_time: "",
    community_code: "",
    status: "",
  });
  initData();
};

// ----------------- 编辑回显 ----------------------------
const edit = (id) => {
  addRef.value.replyData(id);
};
// ----------------- 删除 ----------------------------
const del = async (id) => {
  await delRepairInfoApi(id);
  ElMessage.success("删除成功");
  if (queryData.current > 1 && list.value.length === 1) queryData.current--;
  initData();
};

// -------------------------
// 处理状态文字
const getStatusText = (val) => {
  let res = getRepairStatus.find((item) => item.value === val * 1);
  // console.log(res);
  return res ? res.label : "无效";
};

// 处理时间
const timeArr = ref([]);
watch(timeArr, (val) => {
  console.log(timeArr.value);
  if (timeArr.value) {
    queryData.create_time = val
      .map((item) => {
        item = +new Date(item);
        return item;
      })
      .join(",");
    console.log(queryData.create_time);
  } else {
    queryData.create_time = "";
  }
});
</script>
<template>
  <div>
    <div class="page-box">
      <div class="top">
        <span class="h_tit">报修管理</span>
        <span class="text">用于管理小区里业主的报修信息</span>
      </div>

      <div class="utils">
        <el-form :model="queryData" label-width="80px" inline="true">
          <el-form-item label="小区名称" style="width: 200px">
            <el-select v-model="queryData.community_code" placeholder="请输入">
              <el-option
                v-for="item in getCommunityNameList"
                :key="item.id"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" style="width: 200px">
            <el-select v-model="queryData.status" placeholder="请输入">
              <el-option
                v-for="item in getRepairStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" style="width: 280px">
            <el-date-picker
              v-model="timeArr"
              type="daterange"
              start-placeholder="请输入"
              end-placeholder="请输入"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="onReset">重置</el-button>
            <el-button @click="onSearch" type="primary">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格区域 -->
      <div class="table">
        <el-row type="flex" justify="space-between">
          <div></div>
          <div class="tab_utils">
            <el-icon color="#409EFC" class="no-inherint"> </el-icon>
            <el-icon color="#409EFC" :icon="Share"> </el-icon>
            <el-icon color="#409EFC" class="no-inherint"> </el-icon>
          </div>
        </el-row>
        <br />
        <Table :table="repairTable" :tableData="list" @edit="edit" @del="del">
          <!-- 小区名称 -->
          <template #community.name="{ row }">
            {{ row.community?.name }}
          </template>
          <!-- 报修人员 -->
          <template #member.name="{ row }">
            {{ row.member?.name }}
          </template>
          <!-- 问题图片 -->
          <template #qu_photo="{ row }">
            <ImagePreview :src="row.qu_photo" alt="" :width="50" />
          </template>
          <!-- 状态 -->
          <template #status="{ row }">
            {{ getStatusText(row.status) }}
          </template>
          <!-- 创建时间 -->
          <template #create_time="{ row }">
            {{ formatTimes(row.create_time) }}
          </template>
        </Table>
      </div>

      <!-- 分页区域 -->
      <Pagination
        v-model:pageNum="queryData.current"
        v-model:pageSize="queryData.pageSize"
        :total="total"
        @initData="initData"
      />

      <!-- 新增表单组件 -->
      <addForm ref="addRef" @updateList="initData" />
    </div>
  </div>
</template>
<style scoped>
.page-box {
  margin: 40px;
  min-height: 500px;
  background-color: #fbfbfc;
}

.h_tit {
  font-weight: 700;
  font-size: 18px;
}

.text {
  font-size: 15px;
  margin-left: 20px;
  color: #575858;
}

.top {
  margin: 10px;
}

.utils {
  margin-left: 5px;
  margin-right: 5px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 5px 5px 5px 3px rgba(186, 177, 177, 0.2);
}

.table {
  margin: 5px;
  padding: 20px;
  margin-top: 10px;
  background-color: #fff;
  box-shadow: 5px 5px 5px 3px rgba(186, 177, 177, 0.2);
}
</style>
